package com.gsm.service.impl;

import com.gsm.dao.SysMenuRoleDao;
import com.gsm.dao.SysRoleDao;
import com.gsm.dao.SysUserRoleDao;
import com.gsm.dto.SysRoleDto;
import com.gsm.entity.Result;
import com.gsm.po.SysRole;
import com.gsm.service.SysRoleService;
import com.gsm.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统角色表(SysRole)表服务实现类
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysMenuRoleDao sysMenuRoleDao;

    @Autowired
    private IdUtils idUtils;

    @Override
    public Result insertSysRole(SysRoleDto dto) {
        long roleId = idUtils.nextId();
        int showOrder = sysRoleDao.selectMaxShowOrder();

        SysRole sysRole = SysRole.builder()
                .roleId(roleId)
                .roleName(dto.getRoleName())
                .roleDesc(dto.getRoleDesc())
                .showOrder(showOrder++)
                .status(1)
                .build();
        sysRoleDao.insertSysRole(sysRole);
        return Result.success();
    }

    /**
     * 根据 主键 roleId 删除
     *
     * @param roleId 主键
     * @return
     */
    @Override
    public Result deleteSysRoleByRoleId(Long roleId) {
        sysRoleDao.deleteSysRoleByRoleId(roleId);
        //删除关联信息
        sysUserRoleDao.deleteSysUserRoleByRoleId(roleId);
        sysMenuRoleDao.deleteSysMenuRoleByRoleId(roleId);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param sysRole 实例对象
     * @return
     */
    @Override
    public Result updateSysRoleByRoleId(SysRole sysRole) {
        sysRoleDao.updateSysRoleByRoleId(sysRole);
        return Result.success();
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param roleId 主键
     * @return
     */
    @Override
    public Result selectSysRoleByRoleId(Long roleId) {
        SysRole sysRole = sysRoleDao.selectSysRoleByRoleId(roleId);
        Result result = Result.success(sysRole);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param sysRole 实例对象
     * @return
     */
    @Override
    public Result selectSysRoleList(SysRole sysRole) {
        List<SysRole> list = sysRoleDao.selectSysRoleList(sysRole);
        return Result.success(list);
    }

    @Override
    public Result updateSysRoleShowOrder(List<Long> roleIds) {
        sysRoleDao.updateSysRoleShowOrder(roleIds);
        return Result.success();
    }
}